<?php if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Table_model extends CI_Model {

	function __construct() {
		// Call the Model constructor
		parent::__construct();
		$this->load->database();
	}

	function show_all_table() {
		$this->load->database();
		$this->db->select('table_id');
		$this->db->where('status',0);
		$this->db->where('date_create',date("d-m-Y"));
		$this->db->distinct();
		$this->db->from('tbl_oders');
		$query_table = $this->db->get();
		$tableIDArr=array();
		foreach ($query_table->result() as $id) {
			$tableIDArr[]=$id->table_id;
		}
		$result = array();
		$this->db->order_by("id", "desc");
		$this->db->where('remove !=', 1);
		$query = $this->db->get('tbl_tables');
		if ($query->num_rows() > 0) {
			$result = $query->result();
		}
		foreach ($result as $table) {
			if (in_array($table->id,$tableIDArr) ) {
				$table->status = 0;
			}
			else {
				$table->status = 1;
			}
		}
		return $result;
	}
	function show_all_table_available() {
		$this->load->database();

		$this->db->select('table_id');
		$this->db->where('status',0);
		$this->db->where('date_create',date("d-m-Y"));
		$this->db->distinct();
		$this->db->from('tbl_oders');
		$query_table = $this->db->get();
		$tableIDArr=array();
		foreach ($query_table->result() as $id) {
			$tableIDArr[]=$id->table_id;
		}
		$this->db->where('remove !=', 1);
		$this->db->order_by("id", "desc");
		if (count($tableIDArr)>0){
			$this->db->where_not_in('id',$tableIDArr);
		}
		$query = $this->db->get('tbl_tables');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}

	function show_all_table_available_static() {
		$this->load->database();
		$this->db->select('table_id');
		$this->db->where('status',0);
		$this->db->where('date_create',date("d-m-Y"));
		$this->db->distinct();
		$this->db->from('tbl_oders');
		$query_table = $this->db->get();
		$tableIDArr=array();
		foreach ($query_table->result() as $id) {
			$tableIDArr[]=$id->table_id;
		}
		$this->db->where('remove !=', 1);
		$this->db->order_by("id", "desc");
		if (count($tableIDArr)>0){
			$this->db->where_not_in('id',$tableIDArr);
		}
		$query = $this->db->get('tbl_tables');
		if ($query->num_rows() > 0) {
			return 0;
		}else{
			return 1;
		}
			
	}


	function returntablename($id){
		$this->load->database();
		$this->db->where("id", $id);
		$query = $this->db->get('tbl_tables');
		if ($query->num_rows() > 0) {
			foreach($query->result() as $row){
				return $row->table_name;
			}
		}
		return $query->result();
	}
	function checktable($tablename) {
		$this->load->database();
		$this->db->where(array('table_name' => $tablename));
		$query = $this->db->get('tbl_tables');
		if ($query->num_rows() > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	function load_details($id) {
		$this->load->database();
		$this->db->where("id", $id);
		$query = $this->db->get('tbl_tables');
		if ($query->num_rows() > 0) {
			return $query->result();
		}
		return $query->result();
	}

	function add_table($name,$type) {
		$this->load->database();
		$data = array(
				'table_name' => $name,
				'table_type' => $type
		);
		$this->db->insert('tbl_tables', $data);
	}

	function del_table($id) {
		$this->load->database();
		$data = array(
				'remove' => 1,
		);
		$this->db->where('id', $id);
		$this->db->update('tbl_tables',$data);
	}

	function update_table($id, $name,$type) {
		$data = array(
				'table_name' => $name,
				'table_type' => $type
		);
		$this->db->where('id', $id);
		$this->db->update('tbl_tables', $data);
	}

}

?>